package com.example.a429;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private final List<Goods> goodsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView goodsGridView = findViewById(R.id.goods_grid_view);
        ImageView bigImageView = findViewById(R.id.big_image_view);
        bigImageView.setOnClickListener(v -> Toast.makeText(MainActivity.this, "点击了大图", Toast.LENGTH_SHORT).show());

        // 初始化商品数据
        initGoodsData();

        // 创建适配器
        List<Map<String, Object>> data = new ArrayList<>();
        for (Goods goods : goodsList) {
            Map<String, Object> item = new HashMap<>();
            item.put("image", goods.getImageResId());
            item.put("name", goods.getName());
            item.put("description", goods.getDescription());
            item.put("price", goods.getPrice());
            item.put("location", goods.getLocation());
            data.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.goods_item_layout,
                new String[]{"image", "name", "description", "price", "location"},
                new int[]{R.id.goods_image, R.id.goods_name, R.id.goods_description, R.id.goods_price, R.id.goods_location});
        goodsGridView.setAdapter(adapter);

        goodsGridView.setOnItemClickListener((parent, view, position, id) -> {
            Goods clickedGoods = goodsList.get(position);
            if ("MacBook Pro 2021 14寸".equals(clickedGoods.getName())) {
                // 跳转到商品详情页
                Intent intent = new Intent(MainActivity.this, MainActivity1.class);
                // 传递商品信息
                intent.putExtra("name", clickedGoods.getName());
                intent.putExtra("image", clickedGoods.getImageResId());
                intent.putExtra("description", clickedGoods.getDescription());
                intent.putExtra("price", clickedGoods.getPrice());
                intent.putExtra("location", clickedGoods.getLocation());
                startActivity(intent);
            } else {
                Toast.makeText(MainActivity.this, "点击了 " + clickedGoods.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        // 初始化按钮
        Button btnHome = findViewById(R.id.btn_home);
        Button btnCategory = findViewById(R.id.btn_category);
        Button btnAdd = findViewById(R.id.btn_add);
        Button btnMessage = findViewById(R.id.btn_message);
        Button btnMy = findViewById(R.id.btn_my);

        // 设置按钮点击事件
        btnHome.setOnClickListener(v -> navigateToActivity(MainActivity3.class));
        btnCategory.setOnClickListener(v -> navigateToActivity(MessageActivity.class));
        btnAdd.setOnClickListener(v -> navigateToActivity(PublishGoodsActivity.class));
        btnMessage.setOnClickListener(v -> navigateToActivity(MessageActivity.class));
        btnMy.setOnClickListener(v -> navigateToActivity(Personal.class));
    }

    private void navigateToActivity(Class<?> activityClass) {
        Intent intent = new Intent(this, activityClass);
        startActivity(intent);
    }

    private void initGoodsData() {
        // 添加示例商品数据
        goodsList.add(new Goods("iPhone 13 Pro 256GB", R.drawable.iphone, "9.5新 | 完好无损", 4999, "上海"));
        goodsList.add(new Goods("Nike Air Jordan 1 高帮篮球鞋", R.drawable.shoes, "8.5新 | 轻微磨损", 799, "北京"));
        goodsList.add(new Goods("MacBook Pro 2021 14寸", R.drawable.books, "9新 | 性能完好", 8999, "广州"));
        goodsList.add(new Goods("索尼 WH-1000XM4 降噪耳机", R.drawable.snoy, "9.5新 | 配件齐全", 1599, "深圳"));
    }
}